package com.authorization.service;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;

import com.authorization.feign.UserClients;
import com.ruoyi.common.core.constant.TokenConstants;
import com.ruoyi.common.core.domain.R;
import domain.AdminUser;
import model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SaLoginService {
    @Autowired
    private UserClients userClients;
    public SaResult login(String username, String password) {
        System.out.println("调用了");
        R<LoginUser> loginUser = userClients.getUser(username);
        AdminUser user = loginUser.getData().getAdminUser();
        if (user.getStatus() == 0){
            return SaResult.error("账号已被禁用");
        } else if (user.getStatus() == 1) {
            System.out.println("token=="+SaSecureUtil.aesEncrypt(TokenConstants.SECRET,password));
            if (user.getPassword().equals(SaSecureUtil.aesEncrypt(TokenConstants.SECRET,password))){
                StpUtil.login(user.getUserId());
                System.out.println(StpUtil.getTokenInfo());
                SaResult.get(200,"登录成功",StpUtil.getTokenInfo());
            }else {
                return SaResult.error("密码错误");
            }
        }else {
            return SaResult.error("用户未激活");
        }
        return SaResult.data(StpUtil.getTokenInfo());
    }
}
